#!/bin/sh /etc/rc.common

START=99
USE_PROCD=1

NAME=fsh
PROG=/usr/bin/$NAME

validate_section_fshs() {
	uci_load_validate "${NAME}" fshs "$1" "$2" \
		'enable:bool:0' \
		'addr:string' \
		'port:port' \
		'tokens:string'
}

fshs_instance() {
	[ "$2" = 0 ] || {
		echo "validation failed"
		return 1
	}

	[ "${enable}" = 0 ] && return 1

	procd_open_instance "$1"

	procd_set_param command "$PROG" "-s"
	[ -n "$tokens" ] && [ -e "$tokens" ] && {
		procd_append_param command -a "$tokens"
	}
	procd_append_param command "$addr":"$port"

	procd_set_param limits core="unlimited"
	procd_set_param limits nofile="1000000 1000000"
	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_set_param respawn

	procd_close_instance
}

start_service() {
	config_load "${NAME}"
	config_foreach validate_section_fshs fshs fshs_instance
}

service_triggers() {
	procd_add_reload_trigger "$NAME"

	procd_open_validate
	validate_section_fshs
	procd_close_validate
}
